package com.exonline.front.controller.resource;

import com.exonline.commons.base.IBaseService;
import com.exonline.commons.base.IBaseServlet;
import com.exonline.commons.dao.DaoImpl;
import com.exonline.commons.dao.IDao;
import com.exonline.commons.entity.CoursesResource;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @desc 视频资源管理的M层
 * @author hbrj
 */
public class ResourceService implements IBaseService<CoursesResource> {
    // 创建数据访问对象 用于处理课程相关业务逻辑
   IDao<CoursesResource> ResourceManagementDao = new DaoImpl<>();

    /**
     * @desc  1. 分页查询所有课程信息
     * @auhtor hbrj
     * @param pageSize 每页长度
     * @param pageNo  页数
     * @throws SQLException 如果数据库操作出现错误
     */
    @Override
    public List<CoursesResource> queryAll(int pageSize, int pageNo) throws SQLException {
        // 执行分页查询
        return ResourceManagementDao.queryWithPagingAndConditions("SELECT * FROM role",null,pageNo,pageSize,new BeanListHandler<>(CoursesResource.class));
    }

    /**
     * @desc  2.根据id查询一条课程信息
     * @param id 课程的id
     * @throws SQLException 如果数据库操作出现错误
     */
    @Override
    public CoursesResource queryOne(String id) throws SQLException {
        return ResourceManagementDao.queryForObject("select * from coursesresource where id = ?", new BeanHandler<>(CoursesResource.class), id);
    }

    /**
     * @desc  3. 添加一条课程信息
     * @param obj
     * @throws SQLException 如果数据库操作出现错误
     */
    @Override
    public void add(Object... obj) throws SQLException {
        ResourceManagementDao.updateWithTransaction("INSERT INTO coursesresource(id,cid,videourl) VALUES(?,?,?)",obj);
    }

    /**
     * @desc  4. 修改一条课程信息
     * @param obj
     * @throws SQLException 如果数据库操作出现错误
     */
    @Override
    public void update(Object... obj) throws SQLException {
        ResourceManagementDao.updateWithTransaction("UPDATE coursesresource SET url =? ,WHERE id = ?",obj);
    }

    /**
     * @desc  5. 删除一条课程信息
     * @param id
     * @throws SQLException 如果数据库操作出现错误
     */
    @Override
    public void deleteById(String id) throws SQLException {
        ResourceManagementDao.updateWithTransaction(" DELETE FROM coursesresource WHERE id = ?",id);
    }
}
